package com.wswy.wzcx.widget

import android.content.Context
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.model.pay.PayStyleModel
import com.wswy.wzcx.module.pay.PayManager

class PayLayout : LinearLayout,View.OnClickListener{

    constructor(context: Context):this(context,null)

    constructor(context: Context, attribute: AttributeSet?):this(context,attribute,0)

    constructor(context: Context, attribute: AttributeSet?, defStyle:Int):super(context,attribute,defStyle){
        orientation = LinearLayout.VERTICAL



        val typedArray = context.obtainStyledAttributes(attribute, R.styleable.PayLayout)

        payType = typedArray.getInt(R.styleable.PayLayout_pay_type,PayManager.PAY_TYPE_NORMAL)

        typedArray.recycle()

        initViews()
    }


    private var payType = PayManager.PAY_TYPE_NORMAL

    private var selectedItem:View?=null

    private fun initViews(){
        //setBackgroundColor(Color.WHITE)
        //dividerDrawable =ContextCompat.getDrawable(context,R.drawable.list_divider_h)
        //showDividers = SHOW_DIVIDER_MIDDLE

        //默认第一个选择
        PayManager.getPayStyles(payType).forEachIndexed { index, payStyleModel ->
            addView(getPayView(payStyleModel,index == 0),LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,SizeUtils.dp2px(48f)))

        }
    }

    fun setPayType(type:Int){

        removeAllViewsInLayout()
        payType = type
        PayManager.getPayStyles(payType).forEachIndexed { index, payStyleModel ->
            addView(getPayView(payStyleModel,index == 0),LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,SizeUtils.dp2px(48f)))

        }
    }


    var selectedCallback:((PayStyleModel)->Unit)?=null

    fun getPayStyle():PayStyleModel{

        return (selectedItem?.getTag(R.id.item_data) as? PayStyleModel)?.let {

             it
        }?:kotlin.run {
            PayManager.getDefaultPayStyle(payType)
        }
    }


    private fun getPayView(payStyleModel: PayStyleModel,defSelected:Boolean):View{

        return LayoutInflater.from(context).inflate(R.layout.item_pay_type,this,false).apply {

            val holder = TypeViewHolder().apply {
                imgPay = findViewById<ImageView>(R.id.img_pay)

                tvPayName = findViewById<TextView>(R.id.tv_pay_name)
                imgChoose = findViewById<ImageView>(R.id.img_choose)
            }

            tag = holder

            setTag(R.id.item_data,payStyleModel)

            holder.imgPay.setImageResource(payStyleModel.drawableId)
            holder.tvPayName.text = payStyleModel.title
            holder.imgChoose.isSelected = defSelected

            setOnClickListener(this@PayLayout)
            if (defSelected){
                selectedItem = this
            }
        }
    }

    override fun onClick(v: View?) {
        (selectedItem?.tag as? TypeViewHolder)?.imgChoose?.isSelected = false


        (v?.tag as? TypeViewHolder)?.imgChoose?.isSelected = true

        selectedItem = v


        (v?.getTag(R.id.item_data) as? PayStyleModel)?.let {
            selectedCallback?.invoke(it)
        }
    }


    private class TypeViewHolder {

        lateinit var imgPay: ImageView
        lateinit var tvPayName: TextView
        lateinit var imgChoose: ImageView

    }


}